package com.hyyt.shopjsp.basic.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.basic.vo.BasicStatusRelateVo;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.basic.pojo.BasicKeyBook;
import com.hyyt.shopjsp.basic.service.IBasicKeyBookService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/17 0024下午 1:30.
 */
@Controller
@RequestMapping("/back/keybook")
public class BasicKeyBookController extends BaseController {
	private Logger log = Logger.getLogger(BasicKeyBookController.class);
	private String basePath = "/back/basic/keyBook/";

	@Resource
	private IBasicKeyBookService basicKeyBookService;//数据字典service接口

	/**
	 * 进入数据字典管理
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoKeyBookPage")
	public String gotoKeyBookPage(){
		return basePath+"keyBookList";
	}

	/**
	 * 查询所有数据字典
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="listKeyBook",method=RequestMethod.POST)
	@ResponseBody
	public void listKeyBook(String name, String typeName, Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> param = new HashMap<>();
		if (name!=null&&!"".equals(name.trim())){
			param.put("name",name);
		}
		if (typeName!=null&&!"".equals(typeName.trim())) {
			param.put("typeName", typeName);
		}
		param.put("sortColumns", "keyBookId desc");
		int totalRecordCount =basicKeyBookService.countBasicKeyBook(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<BasicKeyBook> list =basicKeyBookService.queryBasicKeyBook(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据数据字典id获得对象
     *
     * @param keyBookId
     */
    @RequestMapping(value = "getKeyBookInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getKeyBookInfo(Integer keyBookId) {
		if (Utils.objectIsNotEmpty(keyBookId)) {
			BasicKeyBook keybook = basicKeyBookService.loadBasicKeyBook(keyBookId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(keybook));
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 保存或者修改数据字典
     *
     * @param basicKeybook
     * @return
     */
    @RequestMapping(value = "saveOrUpdateKeyBook", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg saveOrUpdateKeyBook(BasicKeyBook basicKeybook) {
		if (Utils.objectIsNotEmpty(basicKeybook)) {
			if (Utils.objectIsEmpty(basicKeybook.getKeyBookId())) {
				basicKeyBookService.saveBasicKeyBook(basicKeybook);
			} else {
				basicKeyBookService.updateBasicKeyBook(basicKeybook);
			}
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 删除传入的数据字典id
     *
     * @param keyBookIds
     */
    @RequestMapping(value = "deleteBasicKeyBook", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteBasicKeyBook(String keyBookIds) {
		if (Utils.objectIsNotEmpty(keyBookIds)) {
			basicKeyBookService.deleteBasicKeyBook(Integer.parseInt(keyBookIds));
			return ResultMsg.create(true);
		}else{
			return ResultMsg.create(false);
		}

	}

    /**
     * 同步数据字典
     * @param response
     */
    @RequestMapping(value="updateInServletContextKeyBook",method=RequestMethod.POST)
    @ResponseBody
	public void updateInServletContextKeyBook(HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		boolean success=false;
		List<String> typeList = basicKeyBookService.getStatusRelateType();//查询要加载的数据（通过type进行了去重）
		if (Utils.collectionIsNotEmpty(typeList)) {
			for (String type : typeList) {
				List<BasicStatusRelateVo> statusRelateVoList = basicKeyBookService.getStatusRelateByType(type);   //通过type查询对应的集合
				if (Utils.collectionIsNotEmpty(statusRelateVoList)) {
					String jsonString = FastJsonUtils.toJSONString(statusRelateVoList);//将value格式化成json格式
					redisBasicService.setKey(StringStaticKey.KEYBOOK_STATUS_PREFIX + type, jsonString); //将查询结果放入redis
				}
			}
			success = true;//查询到数据 将result更改为true
			param.put("success", success);
		}
		ResultMsg.create(param, response);  //把内容发送到页面
	}
}